package com.taomu.app.zongchouhuli.moudle.mine.contract.impl;

import android.text.TextUtils;
import android.widget.TextView;

import com.jakewharton.rxbinding2.widget.RxTextView;
import com.jakewharton.rxbinding2.widget.TextViewAfterTextChangeEvent;
import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.http.api.HttpObserver;
import com.taomu.app.zongchouhuli.http.api.Request;
import com.taomu.app.zongchouhuli.http.res.ResBankEntity;
import com.taomu.app.zongchouhuli.http.res.ResMyBankListEntity;
import com.taomu.app.zongchouhuli.moudle.mine.contract.UserCashContract;
import com.taomu.app.zongchouhuli.utils.StorageHelper;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class UserCashPresenter implements UserCashContract.Presenter {

    private UserCashContract.View mContractView;

    private CompositeDisposable disposable;

    public UserCashPresenter(UserCashContract.View mContractView){
        this.mContractView = mContractView;
        this.disposable = new CompositeDisposable();
    }

    @Override
    public void getCrash(String total_fee, String bank_id) {
        Request.getIntence().getCrash(StorageHelper.getToken(), total_fee, bank_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<String>(mContractView,disposable,true) {
                    @Override
                    public void onNext(String msg) {
                        if(mContractView != null){
                            mContractView.setSubmitResults(msg);
                        }
                    }
                });
    }

    @Override
    public void getBankList() {
        Request.getIntence().getBankList(StorageHelper.getToken(),"0","10")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<ResMyBankListEntity>(mContractView,disposable,true) {
                    @Override
                    public void onNext(ResMyBankListEntity resProductEntities) {
                        if(mContractView != null){
                            mContractView.setBankList(resProductEntities);
                        }
                    }
                });
    }

    @Override
    public void isNextCanClick(TextView tv1, TextView tv2) {
        Observable.combineLatest(isEditTextInputTextObservable(tv1),
                isEditTextInputTextObservable(tv2),
                new BiFunction<Boolean, Boolean, Boolean>() {
                    @Override
                    public Boolean apply(Boolean aBoolean, Boolean aBoolean2) throws Exception {
                        return aBoolean&&aBoolean2;
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if(mContractView != null){
                            if(aBoolean){
                                mContractView.setNextCanClick(R.drawable.shape_next_blue,true);
                            }else{
                                mContractView.setNextCanClick(R.drawable.shape_next_gray,false);
                            }

                        }
                    }
                });
    }

    @Override
    public void detch() {

    }

    private Observable<Boolean> isEditTextInputTextObservable(TextView tv) {
        return RxTextView.afterTextChangeEvents(tv)
                .map(new Function<TextViewAfterTextChangeEvent, Boolean>() {
                    @Override
                    public Boolean apply(TextViewAfterTextChangeEvent event) throws Exception {
                        return !TextUtils.isEmpty(event.view().getText());
                    }
                });
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDetch() {
        mContractView = null;
        if(disposable != null){
            disposable.dispose();
            disposable = null;
        }
    }
}
